배선도 여러번 확인 했는데 확인 부탁드립니다 곧 동영상도 올리겠습니다.
구동동영상있는 블로그입니다.
그리고 같이 보내주셨던 예제코드입니다.
#include <SoftwareSerial.h> // 시리얼 통신을 위한 라이브러리 선언
int RightMotor_E_pin = 5; // 오른쪽 모터의 Enable & PWM
int LeftMotor_E_pin = 6; // 왼쪽 모터의 Enable & PWM
int RightMotor_1_pin = 8; // 오른 쪽 모터 제어선 IN1
int RightMotor_2_pin = 9; // 오른쪽 모터 제어선 IN2
int LeftMotor_3_pin = 10; // 왼쪽 모터 제어선 IN3
int LeftMotor_4_pin = 11; // 왼쪽 모터 제어선 IN4
#define L_Line A3 // 왼쪽 라인트레이서 센서는 A3 핀에 연결
#define C_Line A4 // 가운데 라인트레이서 센서는 A4 핀에 연결
#define R_Line A5 // 오른쪽 라인트레이서 센서는 A5 핀에 연결
int Rmotor_dir = 1;
int Lmotor_dir = 1;
int motor_s = 200; // 모터 속도
int last_Sensor; // 마지막 센서값 저장
void setup(){
Serial.begin(9600);
// 모터 핀은 출력으로 설정
pinMode(RightMotor_E_pin, OUTPUT);
pinMode(LeftMotor_E_pin, OUTPUT);
pinMode(RightMotor_1_pin, OUTPUT);
pinMode(RightMotor_2_pin, OUTPUT);
pinMode(LeftMotor_3_pin, OUTPUT);
pinMode(LeftMotor_4_pin, OUTPUT);
// 라인트레이서 센서는 입력으로 설정
pinMode(L_Line,INPUT);
pinMode(C_Line,INPUT);
pinMode(R_Line,INPUT);
delay(3000);
}
void loop(){
if(digitalRead(L_Line)){ // 왼쪽 센서가 감지되면
motor_role(motor_s,-(motor_s/4)); // 좌측 모터를 1/2속도로 회전
last_Sensor = 2;
}
else if(digitalRead(C_Line)){ // 만약 가운데 센서가 감지되면
motor_role(motor_s,motor_s); // 직진하라
last_Sensor = 0;
}
else if(digitalRead(R_Line)){ // 만약 오른쪽 센서가 감지되면
motor_role(-(motor_s/4),motor_s); // 우측 모터를 1/2속도로 회전
last_Sensor = 1;
}
}
void motor_role(int R_motor, int L_motor){
if(R_motor>0){ // 우측 모터 정회전
digitalWrite(RightMotor_1_pin,Rmotor_dir);
digitalWrite(RightMotor_2_pin,!Rmotor_dir);
}
else if(R_motor<0){ // 우측 모터 역회전
digitalWrite(RightMotor_1_pin,!Rmotor_dir);
digitalWrite(RightMotor_2_pin,Rmotor_dir);
}
else{ // 우측 모터 정지
digitalWrite(RightMotor_1_pin,LOW);
digitalWrite(RightMotor_2_pin,LOW);
}
if(L_motor>0){ // 좌측 모터 정회전
digitalWrite(LeftMotor_3_pin,Lmotor_dir);
digitalWrite(LeftMotor_4_pin,!Lmotor_dir);
}
else if(L_motor<0){ // 좌측 모터 역회전
digitalWrite(LeftMotor_3_pin,!Lmotor_dir);
digitalWrite(LeftMotor_4_pin,Lmotor_dir);
}
else{ // 좌측 모터 정지
digitalWrite(LeftMotor_3_pin,LOW);
digitalWrite(LeftMotor_4_pin,LOW);
}
analogWrite(RightMotor_E_pin,abs(R_motor)); // 우측 모터 속도값
analogWrite(LeftMotor_E_pin,abs(L_motor)); // 좌측 모터 속도값
}
댓글목록
등록된 댓글이 없습니다.